package client

import (
	"context"
	"google.golang.org/grpc"
	"tourism-app/common/proto/user"
)

type Handler func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8082", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := user.NewUserClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func ScenicToEs(ctx context.Context, in *user.ScenicToEsReq) (*user.ScenicToEsResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		es, err := client.ScenicToEs(ctx, in)
		if err != nil {
			return nil, err
		}
		return es, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.ScenicToEsResp), nil
}

func SearchScenic(ctx context.Context, in *user.SearchScenicReq) (*user.SearchScenicResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		scenic, err := client.SearchScenic(ctx, in)
		if err != nil {
			return nil, err
		}
		return scenic, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SearchScenicResp), nil
}
func ScenicItem(ctx context.Context, in *user.ScenicItemReq) (*user.ScenicItemResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		scenic, err := client.ScenicItem(ctx, in)
		if err != nil {
			return nil, err
		}
		return scenic, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.ScenicItemResp), nil
}
func Login(ctx context.Context, in *user.LoginReq) (*user.LoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.Login(ctx, in)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LoginResp), nil
}
func CreateOrder(ctx context.Context, in *user.CreateOrderReq) (*user.CreateOrderResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		order, err := client.CreateOrder(ctx, in)
		if err != nil {
			return nil, err
		}
		return order, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.CreateOrderResp), nil
}
func RealName(ctx context.Context, in *user.RealNameReq) (*user.RealNameResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		name, err := client.RealName(ctx, in)
		if err != nil {
			return nil, err
		}
		return name, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.RealNameResp), nil
}
func UpdateOrderStatus(ctx context.Context, in *user.UpdateOrderStatusReq) (*user.UpdateOrderStatusResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		status, err := client.UpdateOrderStatus(ctx, in)
		if err != nil {
			return nil, err
		}
		return status, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UpdateOrderStatusResp), nil
}
func OrderList(ctx context.Context, in *user.OrderListReq) (*user.OrderListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		list, err := client.OrderList(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.OrderListResp), nil
}
func Refund(ctx context.Context, in *user.RefundReq) (*user.RefundResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		refund, err := client.Refund(ctx, in)
		if err != nil {
			return nil, err
		}
		return refund, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.RefundResp), nil
}
